/*
 * Copyright (c) 2017 Baidu, Inc. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef DUEROS_DCS_APP_SAMPLEAPP_RECORDAUDIOMANAGER_H_
#define DUEROS_DCS_APP_SAMPLEAPP_RECORDAUDIOMANAGER_H_

#include <stdio.h>
#include <pthread.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string>

namespace duerOSDcsApp {
namespace application {

class RecordAudioManager {
public:
    static RecordAudioManager* getInstance();

    static void releaseInstance();

    void transmitAudioRecordData(const void* buffer, unsigned long size);

private:
    RecordAudioManager();

    RecordAudioManager(const RecordAudioManager& oth);

    RecordAudioManager& operator=(const RecordAudioManager& oth);

    ~RecordAudioManager();

    static void init();

    static void release();

    int getCurrentFileIndex();

    bool chooseTargetFile();

    int getIndex(const std::string& fileName, int endIndex);

private:
    static RecordAudioManager* s_instance;
    static pthread_once_t s_initOnce;
    static pthread_once_t s_destroyOnce;
    FILE *m_file;
    unsigned int m_writeCounter;
    std::string m_dirName;
};

}
}

#endif //DUEROS_DCS_APP_SAMPLEAPP_RECORDAUDIOMANAGER_H_
